/*
 *根据用户信息展示页面
 */
var currentIndex=1; //起始页数
var pageSize=30;//一页显示几条新闻
//var newsnum=0;//第几条新闻
var readyover=0;//控制页面滚动 1可以滚动加载 0不可以
var isclick=0;//控制按钮点击状态 0当前没有正在点击的按钮 1有点击的按钮
var p1=0;
var current=0;
var keywordcount=0;//屏蔽关键词个数
var urlcount=0;//屏蔽网址个数
var newscount=0;//屏蔽新闻个数
var newstitle={};//用新闻ID 获取新闻标题
var firstNewsTime="";
var allavawidth=parseInt(window.screen.width)*0.5;
var avawidth=parseInt(parseInt(window.screen.width)/5);//手指滑动距离
var urltypearr=[];//请求新闻的ID
var urltypearrindex=0;//pulunews/GetPageDataByTypeId2
var urlloca="https://www.pulu.com/webapi/api/PuluNews/GetPageDataByTypeId1_withdate?typeId=";//加载的地址链接
//没有用户信息
if($.cookie("MYID")){
    var id= $.cookie("MYID");
    urlloca="https://www.pulu.com/webapi/api/PuluNews/GetPageDataByTypeId1_withdate?userId="+ $.cookie("MYID")+"&typeId=";//加载的地址链接
}else{
    urlloca="https://www.pulu.com/webapi/api/pulunews/GetPageDataByTypeId2?typeId=";
}
//没有来源网址
if(document.referrer&&/subscribe/.test(document.referrer)&&/returnLoad/.test(location.href)){
    var navlist=(location.href).match(/returnLoad=[a-z]+/)[0].split("=")[1];
    $.cookie("comfrom",navlist);//选择类别刷新
}
//没有新闻类别从网址获取
if(!$.cookie("newstypeid")){
    var typeid = location.href.match(/typeid=[0-9]+/)[0].split("=")[1];
}else{
    var typeid = $.cookie("newstypeid");
}
var frometype;
var alltupearr={"2":"国际1","9":"汽车1","1":"国内1","5":"互联网1","7":"房产1","111":"家居1","4":"军事1","161":"天气1","3":"财经1","6":"教育1","11":"体育1","12":"社会1","37":"时尚购物1","42":"健康1","43":"数码1","44":"科学1","107":"情感1","134":"怀孕育儿1","136":"城市1","156":"综艺1","160":"心理1","36":"本地活动1","110":"汽车行情1","162":"宗教1","165":"人物1","166":"高校1","167":"养老1","169":"人文1","172":"职场1","180":"留学1","85":"设计2","62":"石化2","13":"娱乐2","65":"机械五金2","70":"食品2","61":"外贸2","64":"印刷2","66":"电气2","67":"建材2","68":"农林牧渔2","69":"服装纺织2","71":"冶金钢铁2","72":"矿产能源2","74":"纸业2","75":"环保2","76":"家电2","77":"玩具2","78":"会计2","79":"市场销售2","80":"人力资源2","81":"律师2","82":"航空航天2","83":"记者2","84":"播音主持2","96":"广告2","98":"交通2","99":"通信2","100":"塑料2","101":"保险2","102":"医护人员2","103":"公务员2","104":"作家2","105":"教师2","109":"动漫2","115":"IT2","117":"物业管理2","120":"建筑装饰2","123":"电子商务2","124":"鞋业2","125":"警察2","127":"经理人2","130":"园艺2","132":"包装2","133":"培训2","140":"制造业2","141":"零售业2","142":"图书出版2","143":"珠宝业2","148":"陶瓷2","152":"中医2","153":"美容美发2","154":"餐饮2","174":"海洋2","15":"图片3","14":"游戏3","158":"宠物3","59":"摄影3","53":"集邮3","112":"电影3","57":"星座3","54":"鉴赏收藏3","93":"乒乓球3","38":"音乐3","49":"瑜伽3","113":"哲学3","55":"彩票3","94":"排球3","150":"美食3","151":"养生3","159":"戏曲3","163":"国学3","164":"茶道3","168":"书画3","45":"网球3","46":"羽毛球3","47":"游泳3","50":"编织3","51":"花卉3","52":"艺术3","56":"旅游3","58":"读书3","86":"桌球3","91":"篮球3","95":"棋牌3","106":"历史3","108":"公益3","114":"文艺3","137":"足球3","146":"金融理财3","147":"金融理财3","149":"风水3","203":"八卦美女3"};
var typename=$.cookie("newstypename");
typename=alltupearr[typeid].replace(/[0-9]/,"");
if(/1/.test(alltupearr[typeid])){
    frometype="zonghe";
}
else if(/2/.test(alltupearr[typeid])){
    frometype="hangye";
}
else if(/3/.test(alltupearr[typeid])){
    frometype="xingqu";
}
/*
 首页返回
 */
$("#logo1").click(function(){
    if(document.referrer){
        history.go(-1);
    }else{
        location.href="http://www.pulu.com/weixin/pulu.html";
    }
});

//
function IsPC(){
    var userAgentInfo = navigator.userAgent;
    //alert(userAgentInfo);//Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
    }
    return flag;
}
var csswidth="style='width:100%'";
$("#pleasewait").css("padding","0");
$("#pleasewait").css("margin","0");
if(IsPC()){
    $("#pleasewait").css("width","100%");
    $(".SimpleToggle.Toggle.HomeMainFeed").css("width","100%");
    $("#newslistbody").css("overflow","auto");
    csswidth="style='width:100%'";
}

$(".PinnedTopicFeed .photo_wrapper .photo a").append("<img class='topic_photo_img' src='hyimg/"+typeid+".png'>");
$(".PinnedTopicFeed .info_wrapper .info").prepend("<span class='TopicNameSpan TopicName'>"+typename+"</span>");
/*
 *加载类别
 */
var hitNume=0;
function getalltypenews(){
    if($.cookie("MYID")){
        hitNume=0;
        $.ajax({
            type:"get",
            url:subscribelisturl+ $.cookie("MYID"),
            success:function(d){
                $(".shownewstypearr").css("visibility","visible");
                if(!$.cookie("newstypeid")){
                    var typeid = location.href.match(/typeid=[0-9]+/)[0].split("=")[1];
                }else{
                    var typeid = $.cookie("newstypeid");
                }
                var parentId=alltupearr[typeid].match(/[0-9]/)[0];
                var npid;
                parentId=="1"?npid=190:parentId=="2"?npid=191:parentId=="3"?npid=192:npid=190;

                for(var i=0;i< d.Data.length;i++){
                    if(d.Data[i].ishit){
                        hitNume++;
                    }
                    if(typeid==d.Data[i].id){
                        if(d.Data[i].ishit){
                            $("#subscribebtn").html("取消");
                            $("#subscribebtn").attr("class","photo subtn subscribe");
                        }else{
                            $("#subscribebtn").html("订阅");
                            $("#subscribebtn").attr("class","photo subtn");
                        }
                    }
                    if(npid==d.Data[i].parentId){
                        $(".shownewstype ul").append("<li onclick='changenews(this)' data-num='"+d.Data[i].id+"' data-hit='"+d.Data[i].ishit+"'>"+d.Data[i].name+"</li>");
                    }
                }

                ajaxing=false;
                $("#subscribebtn").css("display","block");
            }
        })
    }
}
if($.cookie("MYID")){
    getalltypenews();
}
ajaxing=false;
$("#subscribebtn").click(function(e){
    if($.cookie("MYID")){
        //取消事件冒泡
        var eve=arguments.callee.caller.arguments[0]||event;
        if (eve && eve.stopPropagation) {
            eve.stopPropagation();
        } else if (window.event) {
            window.event.cancelBubble = true;
        }
        if(!ajaxing){
            var num=$.cookie("newstypeid");
            if(!/subscribe/.test($("#subscribebtn").attr("class"))){
                //if(hitNume<9){
                    console.log("订阅");
                    ajaxing=true;
                    //添加类别
                    $.ajax({
                        type:"post",
                        url:addsubscribeurl,
                        data:{"userId": $.cookie("MYID"),"typeid":num},
                        success:function(d){
                            $(".shownewstype ul li").remove();
                            getalltypenews();
                        }
                    })
                //}
                //else{
                //    $("#maxNine").css("display","block");
                //    $("#maxNine .max").css("display","block");
                //    $("#maxNine .min").css("display","none");
                //    setTimeout(function(){
                //        $("#maxNine").css("display","none");
                //    },1000);
                //}
            }
            else if(/subscribe/.test($("#subscribebtn").attr("class"))){
                if(hitNume>1){
                    ajaxing=true;
                    //删除类别
                    $.ajax({
                        type:"post",
                        url:delsubscribeurl,
                        data:{"userId":$.cookie("MYID"),"typeid":num},
                        success:function(d){
                            $(".shownewstype ul li").remove();
                            getalltypenews();
                        }
                    })
                }
                else{
                    $("#maxNine").css("display","block");
                    $("#maxNine .max").css("display","none");
                    $("#maxNine .min").css("display","block");
                    setTimeout(function(){
                        $("#maxNine").css("display","none");
                    },1000);
                }
            }
        }
    }
});

//展开新闻类型列表
$(".shownewstypearr").click(function(e){
    if(e.target.className==="shownewstypearr"){
        $(".shownewstypearr").attr("class","shownewstypearr show");
        $(".shownewstype").fadeIn(500);
    }
    else if(e.target.className==="shownewstypearr show"){
        $(".shownewstypearr").attr("class","shownewstypearr");
        $(".shownewstype").fadeOut(500);
    }
});
//历史城市缓存
var choicecity="";
if(typeid==7){
    $("#choicecity").css("display","block");//选择城市按钮
    if(!haveloadcity){
        $("#citylist").load("newscitylist.html");//加载城市列表
        haveloadcity=true;
    }
    choicecity=$.cookie("choicecity7");
    if($.cookie("choicecityname7")){
        $("#choicecity").html($.cookie("choicecityname7"));//页面展示城市名
    }
}
else if(typeid==36){
    $("#choicecity").css("display","block");//选择城市按钮
    if(!haveloadcity){
        $("#citylist").load("newscitylist.html");//加载城市列表
        haveloadcity=true;
    }
    choicecity=$.cookie("choicecity36");
    if($.cookie("choicecityname36")){
        $("#choicecity").html($.cookie("choicecityname36"));//页面展示城市名
    }
}
else if(typeid==110){
    $("#choicecity").css("display","block");//选择城市按钮
    if(!haveloadcity){
        $("#citylist").load("newscitylist.html");//加载城市列表
        haveloadcity=true;
    }
    choicecity=$.cookie("choicecity110");
    if($.cookie("choicecityname110")){
        $("#choicecity").html($.cookie("choicecityname110"));//页面展示城市名
    }
}
else{
    $("#choicecity").css("display","none");//选择城市按钮
}
//切换新闻
var changnewsing="no";//当前是否正在切换城市
function changenews(e){
    console.log(e.innerHTML);
    choicecity="";
    $(".shownewstypearr").attr("class","shownewstypearr");
    $(".shownewstype").css("display","none");
    if(changnewsing==="no"){
        changnewsing="yes";
        typeid=e.dataset.num;
        if(e.dataset.hit=="false"){
            $("#subscribebtn").html("订阅");
            $("#subscribebtn").attr("class","photo subtn");
        }else if(e.dataset.hit=="true"){
            $("#subscribebtn").html("取消");
            $("#subscribebtn").attr("class","photo subtn subscribe");
        }
        if(typeid==7){
            $("#choicecity").css("display","block");//选择城市按钮
            if(!haveloadcity){
                $("#citylist").load("newscitylist.html");//加载城市列表
                haveloadcity=true;
            }
            choicecity=$.cookie("choicecity7");
            if($.cookie("choicecityname7")){
                $("#choicecity").html($.cookie("choicecityname7"));//页面展示城市名
            }
        }
        else if(typeid==36){
            $("#choicecity").css("display","block");//选择城市按钮
            if(!haveloadcity){
                $("#citylist").load("newscitylist.html");//加载城市列表
                haveloadcity=true;
            }
            choicecity=$.cookie("choicecity36");
            if($.cookie("choicecityname36")){
                $("#choicecity").html($.cookie("choicecityname36"));//页面展示城市名
            }
        }
        else if(typeid==110){
            $("#choicecity").css("display","block");//选择城市按钮
            if(!haveloadcity){
                $("#citylist").load("newscitylist.html");//加载城市列表
                haveloadcity=true;
            }
            choicecity=$.cookie("choicecity110");
            if($.cookie("choicecityname110")){
                $("#choicecity").html($.cookie("choicecityname110"));//页面展示城市名
            }
        }
        else{
            $("#choicecity").css("display","none");//选择城市按钮
        }
        $(".PinnedTopicFeed .photo_wrapper .photo a img").attr("src","hyimg/"+typeid+".png");
        $(".PinnedTopicFeed .info_wrapper .info .TopicNameSpan.TopicName").html(e.innerHTML);
        $.cookie("newstypeid",typeid);
        $.cookie("newstypename",e.innerHTML);
        $(".HomeMainFeed div.newsbox").remove();
        currentIndex=1;
        $("#waiting").css("display","block");
        $("#nonews").css("display","none");
        aj(currentIndex,pageSize);
    }
}
//切换城市
var haveloadcity=false;//已经下载了城市列表
function showcityname(){
    if(typeid==7){
        $("#choicecity").css("display","block");//选择城市按钮
        if(!haveloadcity){
            $("#citylist").load("newscitylist.html");//加载城市列表
            haveloadcity=true;
        }
        choicecity=$.cookie("choicecity7");
        if($.cookie("choicecityname7")){
            $("#choicecity").html($.cookie("choicecityname7"));//页面展示城市名
        }
    }
    else if(typeid==36){
        $("#choicecity").css("display","block");//选择城市按钮
        if(!haveloadcity){
            $("#citylist").load("newscitylist.html");//加载城市列表
            haveloadcity=true;
        }
        choicecity=$.cookie("choicecity36");
        if($.cookie("choicecityname36")){
            $("#choicecity").html($.cookie("choicecityname36"));//页面展示城市名
        }
    }
    else if(typeid==110){
        $("#choicecity").css("display","block");//选择城市按钮
        if(!haveloadcity){
            $("#citylist").load("newscitylist.html");//加载城市列表
            haveloadcity=true;
        }
        choicecity=$.cookie("choicecity110");
        if($.cookie("choicecityname110")){
            $("#choicecity").html($.cookie("choicecityname110"));//页面展示城市名
        }
    }
    else{
        $("#choicecity").css("display","none");//选择城市按钮
    }
}
if($.cookie('MYID')){
    showcityname();
}

$("#maingobackbtn").click(function(){
    history.go(-1);
});
//时间 几天前 几分钟前
function getDateTimeStamp(dateStr){
    dateStr=dateStr.replace(/T/g,' ');
    dateStr=dateStr.replace(/\.[0-9]+/g,'');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
        result="刚刚";
    return result;
}

/**
 *iframe点击
 */
function addEvent(o, evt, func) {
    if (o.addEventListener) o.addEventListener(evt, func, false);
    else if (o.attachEvent) o.attachEvent('on' + evt, func);
}
function addDocClickEvt(e) {
    //展示加载图片
    $(e).fadeIn();
    addEvent(e.contentWindow.document,"click", function(e){
        hrefto(e.target);
    });
}
/*
 //iframe处理图片
 */
function showImg( url,imgid,title,nurl,sitename) {
    frameid = 'frameimg' + imgid;
    window['iframeimg'+imgid] = '<img id="iframeimg"'+imgid+'  data-sitename="'+sitename+'" data-title="'+title+'" data-pagenum="'+currentIndex+'" data-newsimg="'+url+'" data-id="'+imgid+'" data-url="'+nurl+'" src=\''+url+'\' style="width:100%;opacity:0;"/>';
    var newimg='<iframe id="'+frameid+'" onload="addDocClickEvt(this)" src="javascript:parent[\'iframeimg'+imgid+'\'];" style="background:url('+url+') no-repeat center;background-size:cover;z-index:-1;display:none;" frameBorder="0" scrolling="no" width="100%" onclick="console.log(\"click iframe\")"></iframe>';
    return newimg;
}
/*
 //选择城市
 */
var pageScrollTop=0;
$("#choicecity").click(function(){
    pageScrollTop=$(window).scrollTop();
    $("#citybox").css("display","block");
    $("#page_wrapper").css("display","none");
    $(window).scrollTop(0);
});
function changecity(e){
    $("#choicecity").html(e.innerHTML);
    $("#citybox").css("display","none");
    $("#page_wrapper").css("display","block");
    //添加网址拼接参数
    if(e.dataset.num){
        var choicenum=e.dataset.num;
        choicecity="&city="+e.dataset.num;
        if(typeid==7){
            $.cookie("choicecity7",choicecity);
            $.cookie("choicecityname7",e.innerHTML);//选择的城市名 跳转返回使用
        }else if(typeid==36){
            $.cookie("choicecity36",choicecity);
            $.cookie("choicecityname36",e.innerHTML);//选择的城市名 跳转返回使用
        }else if(typeid==110){
            $.cookie("choicecity110",choicecity);
            $.cookie("choicecityname110",e.innerHTML);//选择的城市名 跳转返回使用
        }
        $("#hotcity div p").attr("class","");
        $("#hotcity div p").each(function(){
            if($(this)[0].dataset.num===(choicenum)){
                $(this).attr("class","choiced");
            }
        });
    }else{  //选择的是全国
        choicecity="";
        $.cookie("choicecity7","");
        $.cookie("choicecity36","");
        $.cookie("choicecity110","");
        $.cookie("choicecityname7","全国");
        $.cookie("choicecityname36","全国");
        $.cookie("choicecityname110","全国");
        $("#hotcity div p").attr("class","");
    }

    var cityid=e.dataset.num;//城市ID
    var cityname=e.innerHTML;//城市名称
    //从头加载数据
    currentIndex=1;
    pageSize=30;
    //清除原数据
    $(".newsbox").remove();
    $("#waiting").css("display","block");
    $("#nonews").css("display","none");
    aj(currentIndex,pageSize);

    var choicep=document.querySelectorAll("#historylist p");
    var len=choicep.length;
    var canappend="yes";//能否直接插入
    for(var i= 0;i<len;i++){
        if(choicep[i].dataset.num===cityid){
            canappend="no";
        }
    }
    //能插入新历史
    if(canappend==="yes"){
        //历史是否有6个 第一个要替换掉
        if(len>=6){
            console.log("数量达到限制");
            console.log($("#historylist>p:nth-child(2)"));
            $("#historylist>p:nth-child(2)").remove();
        }
        $(("#historylist")).append("<p onclick='changecity(this)' data-num='"+cityid+"'>"+cityname+"</p>");
    }

    //搜索页面和主页切换
    $("#page").css("display","block");
    $("#searchcitypage").css("display","none");
    //搜索清空
    $("#searchcitypage div").remove();
    //搜索框清空
    $('#searchcityinput').val("");
    //搜索按钮取消按钮 还原（隐藏）
    //搜索按钮隐藏
    $("#searchcity").css("display","none");
    $("#cancelsearchcity").css("display","none");
    //搜索框圆角还原
    $(".search .searchdiv").css("border-radius","5px");
}
//城市右侧导航条转
function citynav(e){
    var t = $("#"+e.dataset.id).offset().top-100;
    $(window).scrollTop(t);//滚动到锚点位置
}
/*
 //城市搜索
 */
//焦点事件
$("#searchcityinput").focus(function(){
    $("#page").css("display","none");
    //取消按钮显示
    $("#cancelsearchcity").css("display","block");
    //搜索页面显示
    $("#searchcitypage").css("display","block");
});
//监听输入框
$('#searchcityinput').bind('input propertychange', function() {
    if($('#searchcityinput').val()){
        //搜索按钮显示
        $("#searchcity").css("display","block");
        //搜索框圆角修改
        $(".search .searchdiv").css("border-radius","5px 0 0 5px");
    }else{
        //搜索按钮隐藏
        $("#searchcity").css("display","none");
        //搜索框圆角还原
        $(".search .searchdiv").css("border-radius","5px");
    }
});
//取消搜索 关闭搜索页面
$("#cancelsearchcity").click(function(){
    //搜索按钮隐藏
    $("#searchcity").css("display","none");
    //搜索框圆角还原
    $(".search .searchdiv").css("border-radius","5px");
    //清空输入框
    $('#searchcityinput').val("");
    //页面切换
    $("#page").css("display","block");
    $("#searchcitypage").css("display","none");
    //取消按钮隐藏
    $("#cancelsearchcity").css("display","none");
    //搜索清空
    $("#searchcitypage div").remove();
})
//点击搜索按钮进行搜索
$("#searchcity").click(function(){
    var val=$('#searchcityinput').val();
    //进行搜索
    var py=pinyin.getFullChars(val).toLowerCase();
    $("#searchcitypage div").remove();
    if(py){
        var reg=new RegExp(py);
        var alllist=$("#citylist div");//所有的城市列表
        var alllen=alllist.length;//所有列表个数
        console.log(alllen);
        for(var i=0;i<alllen;i++){
            if(reg.test(alllist[i].className)){
                console.log(alllist[i]);
                $("#searchcitypage").append($(alllist[i]).clone());
            }else{

            }
        }
    }
});

//关闭城市页面 展示主页面
$("#citybox .header span").click(function(){
    $("#citybox").css("display","none");
    $("#page_wrapper").css("display","block");
    $(window).scrollTop(pageScrollTop);
});

$().ready(function(){
    if($.cookie("totop")&&/\d/.test($.cookie("totop"))){ //定位操作
        console.log("开始定位操作");console.log($.cookie("totop"));
        document.getElementsByClassName("HomeMainFeed")[0].removeChild(document.getElementsByClassName("HomeMainFeed")[0].firstChild);
        $(".newsbox").remove();
        current=parseFloat($.cookie("currentIndex"));
        console.log(current);
        var allpagenum=parseFloat($.cookie("currentIndex"))*parseFloat(pageSize);//一共有多少条新闻
        aj2(1,allpagenum);
        var toheight=parseFloat($.cookie("scrollTop"));
        var thisid=$.cookie("newsid");
        var t=parseFloat($.cookie("totop"));
        $(window).scrollTop(t);
        $(".HomeMainFeed").scrollTop(t);
        document.body.scrollTop=t;
        readyover=1;
        currentIndex=current;//更改以后加载的页码
    }else{
        readyover=1;
        aj(currentIndex,pageSize);
    }
});
$(window).scroll(function(){
    var mainheight=$(".home_feed").height();
    var windowheight=$(window).height();
    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();

    var cz=windowheight-mainheight+5;
    //不是选择城市的 滚动效果
    if($("#citybox").css("display")==="none"){
        if(readyover===1&&chajuli<cz){
            readyover=0;
            //调整加载中
            $("#pleasewait").css("width","auto");
            currentIndex++;
            aj(currentIndex,pageSize);
        }
    }
});
var ajrunning="no";
function loadPage(data){
    var allhtmlword="";
    for(var i=0;i<data.Data.length;i++){
        //num++;
        //截取时间
        var tim="";
        if(data.Data[i].published_date){
            tim=getDateDiff(getDateTimeStamp(data.Data[i].published_date));
        };
        //用新闻的ID 获取新闻的标题
        if(newstitle[data.Data[i].id]){
            //已经保存了新闻标题
        }else{
            //保存新闻标题
            newstitle[data.Data[i].id]=data.Data[i].title;
        }
        if(!tim){tim="刚刚"};
        if(data.Data[i].thumb){
            //var iframeimg=showImg(data.Data[i].thumb,data.Data[i].id,data.Data[i].title,data.Data[i].url,data.Data[i].sitename);
            var iframeimg="<div style='background:url("+data.Data[i].thumb+")no-repeat center;background-size:cover;height:100%;'></div>";
            allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-2' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"'data-pagenum='"+currentIndex+"' data-newsimg='"+data.Data[i].thumb+"' data-id='"+data.Data[i].id+"' data-url='"+data.Data[i].url+"'><a><div class='clr'><div class='info-content rt'><div class='text-content' ><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"' id='new"+data.Data[i].id+"'><img src='../img/newsdel2.png'></span></div></div><div class='pic-content'>"+iframeimg+"</div></div></a></div></div>";
        }else if(!data.Data[i].thumb){
            allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-3' data-id='"+data.Data[i].id+"' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"' data-url='"+data.Data[i].url+"' data-pagenum='"+currentIndex+"'><a><div class='text-content'><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' id='new"+data.Data[i].id+"' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"'><img src='../img/newsdel2.png'></span></div></a></div></div>";
        }
        //if(num===data.Data.length){
        //    $("#pleasewait").before(allhtmlword);
        //    $("iframe body").css("padding","0");
        //    ajrunning="no";//加载完成
        //    isclick=0;
        //}
    }
    $("#pleasewait").before(allhtmlword);
    $("iframe body").css("padding","0");
    ajrunning="no";//加载完成
    isclick=0;
}
function aj(currentIndex,pageSize){
    ajrunning="yes";
    isclick=0;
    $.ajax({
        type:"get",
        datatype:"json",
        url:urlloca+typeid+"&currentIndex="+currentIndex+"&pageSize="+pageSize+choicecity+"&date="+firstNewsTime,
        success:function(data){
            //还原加载区域
            $("#pleasewait").css("padding","0");
            if(data.Data==""||!data.Data){
                $("#waiting").css("display","none");
                $("#nonews").css("display","block");
            }
            else{
                if(data.Data.length<10){
                    $("#waiting").css("display","none");
                    $("#nonews").css("display","block");
                }
                var num=0;
                //所有要添加的字符串
                var allhtmlword="";
                if(currentIndex==1){
                    firstNewsTime=data.Data[0].published_date;
                }
                loadPage(data);
                //for(var i=0;i<data.Data.length;i++){
                //    num++;
                //    //截取时间
                //    var tim="";
                //    if(data.Data[i].published_date){
                //        tim=getDateDiff(getDateTimeStamp(data.Data[i].published_date));
                //    };
                //    //用新闻的ID 获取新闻的标题
                //    if(newstitle[data.Data[i].id]){
                //        //已经保存了新闻标题
                //    }else{
                //        //保存新闻标题
                //        newstitle[data.Data[i].id]=data.Data[i].title;
                //    }
                //    if(!tim){tim="刚刚"};
                //    if(data.Data[i].thumb){
                //        var iframeimg=showImg(data.Data[i].thumb,data.Data[i].id,data.Data[i].title,data.Data[i].url,data.Data[i].sitename);
                //        allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-2' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"'data-pagenum='"+currentIndex+"' data-newsimg='"+data.Data[i].thumb+"' data-id='"+data.Data[i].id+"' data-url='"+data.Data[i].url+"'><a><div class='clr'><div class='info-content rt'><div class='text-content' ><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"' id='new"+data.Data[i].id+"'><img src='../img/newsdel2.png'></span></div></div><div class='pic-content'>"+iframeimg+"</div></div></a></div></div>";
                //    }else if(!data.Data[i].thumb){
                //        allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-3' data-id='"+data.Data[i].id+"' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"' data-url='"+data.Data[i].url+"' data-pagenum='"+currentIndex+"'><a><div class='text-content'><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' id='new"+data.Data[i].id+"' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"'><img src='../img/newsdel2.png'></span></div></a></div></div>";
                //    }
                //}
                if(num===data.Data.length){
                    $("#pleasewait").before(allhtmlword);
                    $("iframe body").css("padding","0");
                    ajrunning="no";//加载完成
                    isclick=0;
                }
                readyover=1;
                changnewsing="no";//切换新闻结束
                $("#newslistbody").scrollLeft(allavawidth);
            }
        }
    });
}
function aj2(currentIndex,pageSize){
    console.log("有定位");
    ajrunning="yes";
    isclick=0;
    if(typeid==7){
        choicecity=$.cookie("choicecity7");
        if($.cookie("choicecityname7")){
            $("#choicecity").html($.cookie("choicecityname7"));//页面展示城市名
        }
    }else if(typeid==36){
        choicecity=$.cookie("choicecity36");
        if($.cookie("choicecityname36")){
            $("#choicecity").html($.cookie("choicecityname36"));//页面展示城市名
        }
    }else if(typeid==110){
        choicecity=$.cookie("choicecity110");
        if($.cookie("choicecityname110")){
            $("#choicecity").html($.cookie("choicecityname110"));//页面展示城市名
        }
    }
    var ajaxnum=0;//加载了几条数据
    $.ajax({
        type:"get",
        datatype:"json",
        url:"https://www.pulu.com/webapi/api/PuluNews/GetPageDataByTypeId1_withdate?typeId="+typeid+"&currentIndex="+currentIndex+"&pageSize="+pageSize+"&userId="+id+choicecity+"&date="+firstNewsTime,
        success:function(data){
            $(".newsbox").remove();
            if(data.Data==""||!data.Data){
                $("#waiting").css("display","none");
                $("#nonews").css("display","block");
                isclick=0;
            }else{
                //所有要添加的字符串
                var allhtmlword="";
                if(currentIndex==1){
                    firstNewsTime=data.Data[0].published_date;
                }
                loadPage(data);
                //for(var i=0;i<data.Data.length;i++){
                //    ajaxnum++;//加载了几条数据
                //    //截取时间
                //    var tim="";
                //    if(data.Data[i].published_date){
                //        tim=getDateDiff(getDateTimeStamp(data.Data[i].published_date));
                //    };
                //    if(!tim){tim="刚刚"}
                //    if(data.Data[i].thumb){
                //        var iframeimg=showImg(data.Data[i].thumb,data.Data[i].id,data.Data[i].title,data.Data[i].url,data.Data[i].sitename);
                //        var iframeimg="<div style='background:url("+data.Data[i].thumb+")no-repeat center;background-size:cover;'></div>";
                //        allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-2' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"'data-pagenum='"+currentIndex+"' data-newsimg='"+data.Data[i].thumb+"' data-id='"+data.Data[i].id+"' data-url='"+data.Data[i].url+"'><a><div class='clr'><div class='info-content rt'><div class='text-content' ><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"' id='new"+data.Data[i].id+"'><img src='../img/newsdel2.png'></span></div></div><div class='pic-content'>"+iframeimg+"</div></div></a></div></div>";
                //    }else if(!data.Data[i].thumb){
                //        allhtmlword+="<div class='newsbox' "+csswidth+" id='"+data.Data[i].id+"'><div class='tpl-3' data-id='"+data.Data[i].id+"' onclick='hrefto(this)' data-sitename='"+data.Data[i].sitename+"' data-url='"+data.Data[i].url+"' data-pagenum='"+currentIndex+"'><a><div class='text-content'><h2><p>"+data.Data[i].title+"</p></h2></div><div class='news-from'><span class='src-net'>"+data.Data[i].sitename+"·"+tim+"</span><span class='sendit rt' onclick='shareit(this)' data-nid='"+data.Data[i].id+"'><img src='../img/newsshare2.png' data-nid='"+data.Data[i].id+"'></span><span class='src-delet rt overflow' id='new"+data.Data[i].id+"' onclick='deleteitfun(this)' data-url='"+data.Data[i].host+","+data.Data[i].sitename+"'><img src='../img/newsdel2.png'></span></div></a></div></div>";}
                //
                //    if(ajaxnum===pageSize){
                //        $("#pleasewait").before(allhtmlword);
                //        var t=parseFloat($.cookie("totop"));
                //        $(window).scrollTop(t);
                //        $(".home_feed").scrollTop(t);
                //        document.body.scrollTop=t;
                //        ajrunning="no";//加载完成
                //        $.cookie("totop","")
                //    }
                //};
                //if(ajaxnum===pageSize){
                    $("#pleasewait").before(allhtmlword);
                    var t=parseFloat($.cookie("totop"));
                    $(window).scrollTop(t);
                    $(".home_feed").scrollTop(t);
                    document.body.scrollTop=t;
                    ajrunning="no";//加载完成
                    $.cookie("totop","")
                //}
                $("#newslistbody").scrollLeft(allavawidth);
            }
        }
    });
}
//跳转 历史定位 新闻详情页
function hrefto(d){
    if($.cookie("MYID")){
        $.cookie("currentIndex","");
        $.cookie("scrollTop","");
        $.cookie("newsid","");
        $.cookie("totop","");
        var urlinfo;
        var nthisid;

        if(d.nodeName==="IMG"||d.nodeName==="DIV"){
            urlinfo=d.dataset.url;
            nthisid=d.dataset.id;
        }
        if(urlinfo){
            var pcUrl=true;
            var errorUrl=/环球网|1905电影网|中国会计视野|编织人生网|IT之家|优姿美女/;
            if(d.dataset.sitename){
                if(errorUrl.test(d.dataset.sitename)){pcUrl=false;}
            }
            var t = $(window).scrollTop();
            $.cookie("newsid",d.dataset.id);
            $.cookie("totop",t);
            //var currentIndex= parseFloat(d.dataset.pagenum);
            $.cookie("currentIndex",currentIndex);
            var scrollTop=$(window).scrollTop();
            $.cookie("scrollTop",scrollTop);
            $.cookie("newstitle",d.dataset.title);
            $.cookie("newsurl",urlinfo);
            //接口问题 直接跳转
            $.ajax({
                type:"post",
                datatype:"json",
                url:addcounturl,
                data:{"newsId": d.dataset.id},
                success:function(d){
                    $.cookie("newsShareId","");
                    if(pcUrl){location.href="newsdetail.html?newsid="+nthisid+"#fromuser="+id;}
                    else{location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+nthisid+"#fromuser="+id;}
                }
            })
        }
    }else{
        location.href="http://www.pulu.com/weixin/login.html";
    }
}
//关闭新闻详情页
$("#iframegoback").click(function(){
    $("#newsiframe").attr("src","");
    $("#seenews").css("display","none");
    $("#page_wrapper").css("display","block");
    //$("body").css("overflow","auto");
    $(".wrapper.home_feed").css("overflow","auto");
});
//详情页面分享操作
$("#iframesharebtn").click(function(e){
    console.log(cansend);
    console.log(newstitle);
    var newsid= e.target.dataset.newsid;
    $("#sharetitleword div").html(newstitle[newsid]);
    $("#seenews").css("display","none");
    $("#shareword").css("display","block");
    //转发确定按钮
    if(cansend==1){
        console.log(cansend);
        sendcommentbtn.onclick=function(){
            cansend=0;
            sendcommentbtn.disabled=true;
            if(commentinput.value){
                if(commentinput.value.match(/^\s+$/g)){
                    $("#sharewaiting").css("display","block");
                    $.ajax({
                        type:"post",
                        datatype:"json",
                        url:addshareurl,
                        data:{"contentid":newsid,"userid":id,"fromuser":"","des":"","contenttype":1},
                        success:function(data){
                            console.log(data);
                            commentinput.value="";
                            shareword.style.display="none";
                            $("#sharewaiting").css("display","none");
                            $("#seenews").css("display","block");
                            $("#collectword").css("display","block");
                            setTimeout(function(){
                                $("#collectword").css("display","none");
                            },1000);
                            clearTimeout();
                            cansend=1;
                            sendcommentbtn.disabled=false;
                        }
                    })
                }else{
                    $.ajax({
                        type:"post",
                        datatype:"json",
                        url:addshareurl,
                        data:{"contentid":newsid,"userid":id,"fromuser":"","des":commentinput.value,"contenttype":1},
                        success:function(data){
                            console.log(data);
                            commentinput.value="";
                            shareword.style.display="none";
                            $("#seenews").css("display","block");
                            $("#collectword").css("display","block");
                            setTimeout(function(){
                                $("#collectword").css("display","none");
                            },1500);
                            clearTimeout();
                            cansend=1;
                            sendcommentbtn.disabled=false;
                        }
                    })
                }
            }else{
                $.ajax({
                    type:"post",
                    datatype:"json",
                    url:addshareurl,
                    data:{"contentid":newsid,"userid":id,"fromuser":"","des":"","contenttype":1},
                    success:function(data){
                        console.log(data);
                        commentinput.value="";
                        shareword.style.display="none";
                        $("#collectword").css("display","block");
                        $("#seenews").css("display","block");
                        setTimeout(function(){
                            $("#collectword").css("display","none");
                        },1500);
                        clearTimeout();
                        cansend=1;
                        sendcommentbtn.disabled=false;
                    }
                })
            }
        };
    }
    //取消
    cancelbtn.onclick=function(){
        commentinput.value="";
        $("#seenews").css("display","block");
        shareword.style.display="none";
    }
});
//判断屏蔽个数
function maxnum(){
    if(newscount>=100){
        $("#deleteit").html("屏蔽新闻已达上限");
    }else{
        $("#deleteit").html("屏蔽该新闻");
    };
    if(urlcount>=100){
        $("#deleteurl").html("屏蔽网站已达上限");
    }else{
        $("#deleteurl").html("屏蔽该网站");
    };
    if(keywordcount>=100){
        $("#inputkey").attr("placeholder","屏蔽关键词已达上限");
        $("#inputkey").attr({"disabled":"disabled"});
        $("#sendword").addClass("maxnum");
    }else{
        $("#inputkey").attr("placeholder","");
        $("#inputkey").removeAttr("disabled");
        $("#sendword").removeClass("maxnum");
    }
}
//屏蔽操作
var spanx;
var urlinfo;
function deleteitfun(spanx){
    //取消事件冒泡
    var e=arguments.callee.caller.arguments[0]||event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容
    if (e && e.stopPropagation) {
        // this code is for Mozilla and Opera
        e.stopPropagation();
    } else if (window.event) {
        // this code is for IE
        window.event.cancelBubble = true;
    }
    if(e.target.dataset.url||e.target.nodeName==="IMG"){
        if(e.target.nodeName==="IMG"){
            thisid=e.target.parentNode.id.match(/([0-9]+)/)[0];
            urlinfo=e.target.parentNode.dataset.url;
        }else{
            thisid=e.target.id.match(/([0-9]+)/)[0];
            urlinfo=e.target.dataset.url;
        }
        <!--模态框淡入淡出-->
        var overflowlist=document.getElementsByClassName("overflow");
        var modalbg=document.getElementsByClassName("modal_bg")[0];
        var modal=document.getElementsByClassName("action_menu_container")[0];
        var cancel=document.getElementsByClassName("cancel")[0];
        var timer;
        $(".modal_bg").css("display","block");
        $(".action_menu_container").css("display","block");
        $("#inputkey").val("");//清空输入框上次数据
        cancel.onclick=function(){
            if(modalbg.style.display=="block"&&modal.style.display=="block"){
                modalbg.className+=' animate_out';
                modal.className+=' animate_out';
            }
            timer=setTimeout(changeclass,100);
        };
        function changeclass(){
            modalbg.className='modal_bg';
            modal.className='action_menu_container';
            clearTimeout();
            timer=null;
            changedisplay();
        }
        function changedisplay(){
            modalbg.style.display="none";
            modal.style.display="none";
        }
        //屏蔽该新闻
        $("#deleteit").click(function(){
            console.log("ajrunning",ajrunning);
            console.log("isclick",isclick);
            if(isclick===0&&newscount<100){
                isclick=1;
                if(modalbg.style.display=="block"&&modal.style.display=="block"){
                    modalbg.className+=' animate_out';
                    modal.className+=' animate_out';
                }
                timer=setTimeout(changeclass,100);
                console.log("屏蔽该新闻内部id："+thisid);
                $.ajax({
                    type:"post",
                    datatype:"json",
                    url:"https://www.pulu.com/webapi/api/PuluShield/Addpulu_shield",
                    data:{"userId":id,"content":thisid,"shieldtype":3},
                    success:function(data){
                        console.log(data);
                        console.log(thisid);
                        if(ajrunning==="no"){//如果加载完成
                            $("div#"+thisid).remove();
                            isclick=0;
                            newscount++;
                            thisid=null;
                            var newsboxlen=document.getElementsByClassName("newsbox").length;
                            //页面剩余新闻小于等于5个
                            if(newsboxlen<=5){
                                isclick=0;
                                currentIndex++;
                                aj(currentIndex,pageSize);
                            }
                        }
                    }
                })
            }

        });
        //屏蔽该网址
        $("#deleteurl").click(function(e){
            if(isclick===0&&urlcount<100){
                isclick=1;
                if(modalbg.style.display=="block"&&modal.style.display=="block"){
                    modalbg.className+=' animate_out';
                    modal.className+=' animate_out';
                }
                timer=setTimeout(changeclass,100);
                console.log("屏蔽网站内部id："+thisid);
                console.log(urlinfo);
                $.ajax({
                    type:"post",
                    datatype:"json",
                    url:"https://www.pulu.com/webapi/api/PuluShield/Addpulu_shield",
                    data:{"userId":id,"content":urlinfo,"shieldtype":2},
                    success:function(da){
                        urlinfo=null;
                        $.cookie("currentIndex","");
                        $.cookie("scrollTop","");
                        $.cookie("newsid","");
                        $.cookie("totop","");
                        currentIndex=1;
                        pageSize=30;
                        $(".HomeMainFeed div").remove();
                        aj(currentIndex,pageSize);
                    }
                })
            }
        });
        //屏蔽关键词
        $("#sendword").click(function(){
            //alert("点击操作");
            if(isclick===0&&keywordcount<100){
                if($("#inputkey").val()&&$("#inputkey").val().length>0&&!$("#inputkey").val().match(/\s/)){
                    isclick=1;
                    $.ajax({
                        type:"post",
                        datatype:"json",
                        url:"https://www.pulu.com/webapi/api/PuluShield/Addpulu_shield",
                        data:{"userId":id,"content":$("#inputkey").val(),"shieldtype":1},
                        success:function(da){
                            console.log("屏蔽关键词");
                            console.log(da);
                            timer=setTimeout(changeclass,100);
                            isclick=0;
                            keywordcount++;
                        }
                    })
                }
            }

        });
    }
}
//分享
function shareit(e){
    //取消事件冒泡
    var e=arguments.callee.caller.arguments[0]||event;
    if (e && e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
    if($.cookie("MYID")){
        //转发
        if(e.target.dataset.nid){
            $.cookie("sharenewsid",e.target.dataset.nid);
            location.href="sendsharepage.html?type=1";
        }
    }else{
        location.href="http://www.pulu.com/weixin/login.html";
    }

}
//新改写转发
var cansend=1;// 1可以转发 0不可以转发
//控制屏蔽关键词按钮样式变化
$(function(){
    $("#sendword").bind('input propertychange', function() {
        //$("#sendword").css("init");
        $('#sendword').attr('class','init');
        document.getElementById("sendword").className="init";
    });
});